﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PersonInfo
{
    public partial class Form1 : Form
    {
        private string fileName;

        public Form1()
        {
            InitializeComponent();
        }

        private void fNameTxtb_Validating(object sender, CancelEventArgs e)
        {
            ValidateName(fNameTxtb.Text, sender);
        }

        private void mNameTxtb_Validating(object sender, CancelEventArgs e)
        {
            ValidateName(mNameTxtb.Text, sender);
        }

        private void lNameTxtb_Validating(object sender, CancelEventArgs e)
        {
            ValidateName(lNameTxtb.Text, sender);
        }

        private bool ValidateName(string name, object sender)
        {
            if (((TextBox)sender == mNameTxtb && name == "") || 
                ValidationUtils.IsNameValid(name))
            {
                errorProvider.SetError((TextBox)sender, "");
                return true;
            }
            else
            {
                errorProvider.SetError((TextBox)sender, string.Format("Invalid {0}Name!",((TextBox)sender).Name.First()));
                return false;
            }
        }

        private void ssnTxtb_Validating(object sender, CancelEventArgs e)
        {
            ValidateSSN();
        }

        private bool ValidateSSN()
        {
            if (ValidationUtils.IsSSNValid(ssnTxtb.Text))
            {
                errorProvider.SetError(ssnTxtb, "");
                return true;
            }
            else
            {
                errorProvider.SetError(ssnTxtb, string.Format("Invalid SSN!"));
                return false;
            }
        }

        private void emailTxtb_Validating(object sender, CancelEventArgs e)
        {
            ValidateEmail();
        }

        private bool ValidateEmail()
        {
            if (ValidationUtils.IsEmailValid(emailTxtb.Text))
            {
                errorProvider.SetError(emailTxtb, "");
                return true;
            }
            else
            {
                errorProvider.SetError(emailTxtb, "Invalid E-mail!");
                return false;
            }
        }

        private void phoneTxtb_Validating(object sender, CancelEventArgs e)
        {
            ValidatePhone();
        }

        private bool ValidatePhone()
        {
            if (phoneTxtb.Text == "" || ValidationUtils.IsPhoneValid(phoneTxtb.Text))
            {
                errorProvider.SetError(phoneTxtb, "");
                return true;
            }
            else
            {
                errorProvider.SetError(phoneTxtb, "Invalid Telephone!");
                return false;
            }
        }

        private void okBtn_Click(object sender, EventArgs e)
        {
            if (ValidateForm())
            {
                Save();
            }
            else
            {
                StringBuilder allErrors = new StringBuilder();
                foreach (Control control in this.Controls)
                {
                    string errorMsg = errorProvider.GetError(control);
                    if (!String.IsNullOrEmpty(errorMsg))
                    {
                        allErrors.AppendLine(" - " + errorMsg);
                    }
                    else if (control.Name == fNameTxtb.Name || control.Name == lNameTxtb.Name || control.Name == ssnTxtb.Name || control.Name == emailTxtb.Name)                  
                    {
                        allErrors.AppendLine(" - Invalid " + control.Name);
                    }
                }

                // Show error message and do not close the dialog
                MessageBox.Show(
                    "Please ented valid values in all fields!\nList of errors:\n" + allErrors,
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private bool ValidateForm()
        {
            if (fNameTxtb.Text == "" || lNameTxtb.Text == "" || ssnTxtb.Text == "" || emailTxtb.Text == "")
            {
                return false;
            }
            foreach (Control control in this.Controls)
            {
                if (errorProvider.GetError(control) != "")
                {
                    return false;
                }
            }
            return true;
        }

        private void Save()
        {
        }
    }
}
